import request from '@/utils/request'
import { getDepartmentSelector } from '@/api/permission/department'
export default {
  components: {},
  data() {
    return {
      loading: false,
      visible: false,
      isDetail: false,
      dataForm: {
        id: '',
        photoPath: [],
        name: undefined,
        departmentId: undefined,
        gender: undefined,
        category: undefined,
        lecturerIntroduction: undefined,
        lecturerLevel: undefined,
        enabledMark: 0
      },
      departmentTreeData: [],
      genderOptions: [],
      categoryOptions: [
        {
          enCode: '内部讲师',
          fullName: '内部讲师'
        },
        {
          enCode: '特聘讲师',
          fullName: '特聘讲师'
        }
      ],
      lecturerLevelOptions: [
        {
          enCode: '初级1',
          fullName: '初级1'
        },
        {
          enCode: '初级2',
          fullName: '初级2'
        },
        {
          enCode: '中级1',
          fullName: '中级1'
        },
        {
          enCode: '中级2',
          fullName: '中级2'
        },
        {
          enCode: '高级1',
          fullName: '高级1'
        },
        {
          enCode: '高级2',
          fullName: '高级2'
        }
      ],
      rules: {
        name: [
          {
            required: true,
            message: '请输入姓名',
            trigger: 'change'
          }
        ],
        category: [
          {
            required: true,
            message: '请选择类别',
            trigger: 'change'
          }
        ]
      }
    }
  },
  created() {
    this.$store.dispatch('base/getDictionaryData', { sort: 'sex' }).then(res => {
      this.genderOptions = JSON.parse(JSON.stringify(res))
    })
  },
  mounted() {
    getDepartmentSelector().then(res => {
      this.departmentTreeData = res.data.list
    })
  },
  methods: {
    goBack() {
      this.$emit('refresh')
    },
    init(row) {
      this.dataForm.id = row ? row.id : 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['elForm'].resetFields()
        if (this.dataForm.id) {
          request({
            url: '/api/train/lecturer/' + this.dataForm.id,
            method: 'get'
          }).then(res => {
            this.dataForm = res.data
            this.dataForm.enabledMark = this.dataForm.enabledMark === 1 ? true : false
          })
        }
      })
    },
    dataFormSubmit() {
      this.$refs['elForm'].validate(valid => {
        if (valid) {
          this.dataForm.enabledMark = this.dataForm.enabledMark === true ? 1 : 0
          if (!this.dataForm.id) {
            request({
              url: `/api/train/lecturer`,
              method: 'post',
              data: this.dataForm
            }).then(res => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1000,
                onClose: () => {
                  this.visible = false
                  this.$emit('refresh', true)
                }
              })
            })
          } else {
            request({
              url: '/api/train/lecturer/' + this.dataForm.id,
              method: 'PUT',
              data: this.dataForm
            }).then(res => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1000,
                onClose: () => {
                  this.visible = false
                  this.$emit('refresh', true)
                }
              })
            })
          }
        }
      })
    }
  }
}
